import java.io.*;

/**
 * @author zyj
 * @date 2022年05月30日 16:36
 */
public class CopyFile {
    public static void main(String[] args) {
        copy(new File("C:\\Users\\Lenovo\\Desktop\\20220530"),
                new File("C:\\Users\\Lenovo\\Desktop\\a"));
    }

    private static void copy(File source, File dest) {
        if (source.isDirectory()){
            File[] files = source.listFiles();
            for(File f:files){
                if (f.isDirectory()){
                    File  destFile = getFilePath(f,dest.getPath());
                    if (!destFile.exists()){
                        destFile.mkdirs();
                    }
                    copy(f,destFile);
                }else {
                    copyFile(f,getFilePath(f,dest.getPath()));
                }
            }
        }else {
            copyFile(source,getFilePath(source,dest.getPath()));
        }

    }

    //获取目标文件的文件对象

    /**
     * f:源文件对象
     * pathName：目标文件的父路径
     * @param f
     * @param pathName
     * @return
     */
    private static File getFilePath(File f,String pathName){
        return new File(pathName + File.separator + f.getName());
    }

    //复制文件
    private static void copyFile(File sourceFile, File dest) {
        BufferedReader br = null;
        InputStreamReader isr = null;
        InputStream is = null;

        BufferedWriter bw = null;
        OutputStreamWriter osw = null;
        OutputStream os = null;

        try {
            is = new FileInputStream(sourceFile);
            isr = new InputStreamReader(is);
            br = new BufferedReader(isr);

            os = new FileOutputStream(dest, true);
            osw = new OutputStreamWriter(os);
            bw = new BufferedWriter(osw);
            String s = null;
            while ((s = br.readLine()) != null) {
                bw.write(s);
                bw.newLine();
                bw.flush();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            close(bw);
            close(osw);
            close(os);
            close(br);
            close(isr);
            close(is);
        }
    }


    static void close(OutputStream os) {
        try {
            if (null != os) {
                os.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    static void close(InputStream is) {
        try {
            if (null != is) {
                is.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    static void close(Writer writer) {
        try {
            if (null != writer) {
                writer.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    static void close(Reader reader) {
        try {
            if (null != reader) {
                reader.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }



}
